package com.gemme.gemme.wode;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.fragment.BaseLazyFragment;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.setting.Setting2Activity;
import com.gemme.gemme.util.MobileUtil;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.TvTvArrow;

import java.util.HashMap;
import java.util.List;

import static android.app.Activity.RESULT_OK;


/**
 * Created by sp on 2019/7/18.
 * 首页
 */
public class Wode2Fragment extends BaseLazyFragment implements View.OnClickListener {
    View inflate;
    Context mContext;
    TvTvArrow tta_dingdan;
    View ll_fukuan;
    View ll_fahuo;
    View ll_shouhuo;
    View ll_pingjia;
    View ll_tuikuan;
    TextView tv_fukuan;
    TextView tv_fahuo;
    TextView tv_shouhuo;
    TextView tv_pingjia;
    TextView tv_tuikuan;
    TvTvArrow tta_ptb;
    TvTvArrow tta_xyd;
    TvTvArrow tta_zkj;
    TvTvArrow tta_wdsc;
    TvTvArrow tta_tjg;
    TvTvArrow tta_hyqy;
    TvTvArrow tta_phcx;
    TvTvArrow tta_dizhi;
    TvTvArrow tta_zuji;
    TvTvArrow tta_shezhi;
    View iv_vip;
    TextView tv_name;
    TextView tv_desc;
    TextView tv_yaoqing;
    ImageView iv_head;
    ImageView iv_message;
    CountOrderBean countOrderBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        inflate = inflater.inflate(R.layout.fragment_wode_2, container, false);

        mContext = Wode2Fragment.this.getContext();

        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {

        iv_message = inflate.findViewById(R.id.iv_message);
        iv_vip = inflate.findViewById(R.id.iv_vip);
        tv_name = inflate.findViewById(R.id.tv_name);
        tv_desc = inflate.findViewById(R.id.tv_desc);
        iv_head = inflate.findViewById(R.id.iv_head);
        tv_yaoqing = inflate.findViewById(R.id.tv_yaoqing);

        //订单
        tta_dingdan = inflate.findViewById(R.id.tta_dingdan);

        ll_fukuan = inflate.findViewById(R.id.ll_fukuan);
        ll_fahuo = inflate.findViewById(R.id.ll_fahuo);
        ll_shouhuo = inflate.findViewById(R.id.ll_shouhuo);
        ll_pingjia = inflate.findViewById(R.id.ll_pingjia);
        ll_tuikuan = inflate.findViewById(R.id.ll_tuikuan);

        tv_fukuan = inflate.findViewById(R.id.tv_fukuan);
        tv_fahuo = inflate.findViewById(R.id.tv_fahuo);
        tv_shouhuo = inflate.findViewById(R.id.tv_shouhuo);
        tv_pingjia = inflate.findViewById(R.id.tv_pingjia);
        tv_tuikuan = inflate.findViewById(R.id.tv_tuikuan);

        tta_ptb = inflate.findViewById(R.id.tta_ptb);
        tta_xyd = inflate.findViewById(R.id.tta_xyd);
        tta_zkj = inflate.findViewById(R.id.tta_zkj);
        tta_wdsc = inflate.findViewById(R.id.tta_wdsc);
        tta_tjg = inflate.findViewById(R.id.tta_tjg);
        tta_hyqy = inflate.findViewById(R.id.tta_hyqy);
        tta_phcx = inflate.findViewById(R.id.tta_phcx);
        tta_dizhi = inflate.findViewById(R.id.tta_dizhi);
        tta_zuji = inflate.findViewById(R.id.tta_zuji);
        tta_shezhi = inflate.findViewById(R.id.tta_shezhi);

        tta_dingdan.setLeftText("我的订单").setRightText("查看全部").setArrowVisble(true);
        tta_ptb.setLeftImage(R.drawable.pc_ptb).setLeftText("仙气值");
        tta_xyd.setLeftImage(R.drawable.pc_xyd).setLeftText("心愿单");
        tta_zkj.setLeftImage(R.drawable.pc_zkj).setLeftText("折扣劵");
        tta_wdsc.setLeftImage(R.drawable.pc_wdsc).setLeftText("我的收藏");
        tta_tjg.setLeftImage(R.drawable.pc_tjg).setLeftText("推荐官");
        tta_hyqy.setLeftImage(R.drawable.pc_tjg).setLeftText("会员权益");
        tta_phcx.setLeftImage(R.drawable.pc_chaxun_zhengping).setLeftText("批号查询");
        tta_dizhi.setLeftImage(R.drawable.pc_address).setLeftText("地址");
        tta_zuji.setLeftImage(R.drawable.pc_zuji).setLeftText("足迹");
        tta_shezhi.setLeftImage(R.drawable.pc_shezhi).setLeftText("设置");


        iv_message.setOnClickListener(this);
        iv_vip.setOnClickListener(this);
        tv_yaoqing.setOnClickListener(this);

        tta_dingdan.setOnClickListener(this);
        ll_fukuan.setOnClickListener(this);
        ll_fahuo.setOnClickListener(this);
        ll_shouhuo.setOnClickListener(this);
        ll_pingjia.setOnClickListener(this);
        ll_tuikuan.setOnClickListener(this);
        tta_hyqy.setOnClickListener(this);

        tta_ptb.setOnClickListener(this);
        tta_xyd.setOnClickListener(this);
        tta_zkj.setOnClickListener(this);
        tta_wdsc.setOnClickListener(this);
        tta_tjg.setOnClickListener(this);
        tta_phcx.setOnClickListener(this);
        tta_dizhi.setOnClickListener(this);
        tta_zuji.setOnClickListener(this);
        tta_shezhi.setOnClickListener(this);
        tv_desc.setOnClickListener(this);


    }

    private void getCountOrderDetailId() {
        String url = DataManager.BaseUrl + "mall/shoporderdetail/countOrderDetailId";
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey, DataManager.getToken(mContext));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                /*tv_name
                        iv_head*/
                try {
                    System.out.println();

                    countOrderBean = JSON.parseObject(response, CountOrderBean.class);
                    setCount();
                } catch (Exception e) {

                }
            }
        });
    }

    private void setCount() {
        try {
            tv_fukuan.setVisibility(View.GONE);
            tv_fahuo.setVisibility(View.GONE);
            tv_shouhuo.setVisibility(View.GONE);
            tv_pingjia.setVisibility(View.GONE);
            tv_tuikuan.setVisibility(View.GONE);

            List<CountOrderBean.DataBean> data = countOrderBean.getData();
            for (int i = 0; i < data.size(); i++) {
                CountOrderBean.DataBean dataBean = data.get(i);
                int statusId = dataBean.getStatusId();
                int countDeatail = dataBean.getCountDeatail();
                if (countDeatail <= 0) {
                    continue;
                }
                switch (statusId) {
                    case 0:
                        tv_fukuan.setVisibility(View.VISIBLE);
                        tv_fukuan.setText(countDeatail + "");
                        break;
                    case 1:
                        tv_fahuo.setVisibility(View.VISIBLE);
                        tv_fahuo.setText(countDeatail + "");
                        break;
                    case 2:
                        tv_shouhuo.setVisibility(View.VISIBLE);
                        tv_shouhuo.setText(countDeatail + "");
                        break;
                    case 4:
                        tv_pingjia.setVisibility(View.VISIBLE);
                        tv_pingjia.setText(countDeatail + "");
                        break;
                    case 5:
                        tv_tuikuan.setVisibility(View.VISIBLE);
                        tv_tuikuan.setText(countDeatail + "");
                        break;
                }
            }

        } catch (Exception e) {

        }
    }

    private void getData() {
//        DataManager.refreshToken(mContext);  todo
//        todo 需要刷新token
//        http://192.168.1.7:9999/user/userapp/getuserinfo
        Uri.Builder builder = Uri.parse(DataManager.BaseUrl + "user/userapp/getuserinfo").buildUpon();
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey, DataManager.getToken(mContext));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(mContext, builder.toString(), DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                /*tv_name
                        iv_head*/
                try {
                    System.out.println();
                    WodeInfoBean wodeInfoBean = JSON.parseObject(response, WodeInfoBean.class);
                    String nickname = wodeInfoBean.getData().getNickname();
                    String mobile = wodeInfoBean.getData().getMobile();
                    tv_name.setText(StrUtil.noNUll(nickname).equals("") ?
                            MobileUtil.hideMiddleMobile(StrUtil.noNUll(mobile)) : StrUtil.noNUll(nickname));
                    String headImgUrl = StrUtil.noNUll(wodeInfoBean.getData().getHeadImgUrl());
                    if (!TextUtils.isEmpty(headImgUrl))
                        Glide.with(mContext).load(StrUtil.noNUll(headImgUrl)).transform(new GlideCircleTransform(mContext))
                                .into(iv_head);
                } catch (Exception e) {

                }
            }
        });
    }


    @Override
    public void onLazyLoad() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getData();
                //http://47.114.162.17:9999/mall/shoporderdetail/countOrderDetailId
                getCountOrderDetailId();
            }
        }).start();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_vip:
//                startActivity(new Intent(mContext,VipActivity.class));
                startActivity(new Intent(mContext, Vip3Activity.class));
                break;

            case R.id.iv_message:
//                startActivity(new Intent(mContext,VipActivity.class));
                startActivity(new Intent(mContext, Message2Activity.class));
                break;

            case R.id.tv_yaoqing:
//                startActivity(new Intent(mContext,VipActivity.class));
                startActivity(new Intent(mContext, Tuijianguan2Activity.class));
                break;
            case R.id.tta_dingdan:
                Intent intent = new Intent(mContext, WoDingdanNewActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
                break;
            case R.id.ll_fukuan:
                Intent intent1 = new Intent(mContext, WoDingdanNewActivity.class);
                intent1.putExtra("type", 1);
                startActivity(intent1);
                break;
            case R.id.ll_fahuo:
                Intent intent2 = new Intent(mContext, WoDingdanNewActivity.class);
                intent2.putExtra("type", 2);
                startActivity(intent2);
                break;
            case R.id.ll_shouhuo:
                Intent intent3 = new Intent(mContext, WoDingdanNewActivity.class);
                intent3.putExtra("type", 3);
                startActivity(intent3);
                break;
            case R.id.ll_pingjia:
                Intent intent4 = new Intent(mContext, WoDingdanNewActivity.class);
                intent4.putExtra("type", 4);
                startActivity(intent4);
                break;
            case R.id.ll_tuikuan:
                startActivity(new Intent(mContext, TuikuanListActivity.class));
                break;
            case R.id.tta_ptb:
                startActivity(new Intent(mContext, PlatformMoneyNewActivity.class));
                break;
            case R.id.tta_xyd:
                startActivity(new Intent(mContext, XinYuanDanActivity.class));
                break;
            case R.id.tta_zkj:
                startActivity(new Intent(mContext, WoJuanActivity.class));
                break;

            case R.id.tta_wdsc:
                startActivity(new Intent(mContext, ShoucangActivity.class));
                break;

            case R.id.tta_tjg:
                startActivity(new Intent(mContext, Tuijianguan2Activity.class));
                break;
            case R.id.tta_hyqy:
                startActivity(new Intent(mContext, Vip3Activity.class));
                break;
            case R.id.tta_phcx:
                startActivity(new Intent(mContext, ChaXunZhengPingActivity.class));
                break;

            case R.id.tta_dizhi:
                startActivity(new Intent(mContext, AddressNewActivity.class));
                break;
            case R.id.tta_zuji:
                startActivity(new Intent(mContext, Recode2Activity.class));
                break;

            case R.id.tta_shezhi:
                startActivityForResult(new Intent(mContext, Setting2Activity.class), 111);
                break;

            case R.id.tv_desc:
                startActivity(new Intent(mContext, WodeDetActivity.class));
                break;
        }
    }

    /**
     * 获取返回参数
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) return;

        if (111 == requestCode) { //退出登录
            MainActivity.getTabUI().setTab(0);
        }
    }
}
